<template>
  <view class="container">
    <!-- 背景图片 -->
    <image
        src="/static/bj-sy.jpg"
        mode="aspectFill"
        class="bg-image">
    </image>

    <!-- 查询容器 -->
     <view class="text-container">
        <text class="hint-text">请输入您的身份证号</text>
        <text class="hint-text">以查询比赛结果</text>
      </view>
      <view class="input-wrapper">
        <input class="search-input" 
               v-model="queryParams.idNumber"
               placeholder=""
               maxlength="20"
               type="idcard"
               @confirm="handleSearch"
               @input="onInputChange" />
        <view v-if="queryParams.idNumber && queryParams.idNumber.length > 0" 
              class="clear-btn" 
              @click.stop="handleClear"
              @touchstart.stop="handleClear">
          <text class="clear-icon">×</text>
        </view>
      </view>

    <view class="content-wrapper">
      <view class="upload-bm" >
        <image src="/static/images/yunduo.png" class="yunduo-image"></image>
        <text class="signup-text" @click="handleIndex">确定</text>
      </view>
    </view>


  </view>
</template>

<script>

export default {
  data() {
    return {
      loading: false,
      queryParams: {
        idNumber: '',
      },
      scrollHeight: 0 // 新增滚动区域高度
    }
  },
  filters: {
    formatIdCard(val) {
      return val.replace(/(\d{4})\d{10}(\w{4})/, '$1****$2')
    }
  },
  methods: {
    handleSearch() {
    },

    handleClear(e) {
      // 阻止事件冒泡
      if (e) {
        e.stopPropagation && e.stopPropagation()
        e.preventDefault && e.preventDefault()
      }
      // 强制清空输入框
      this.$set(this.queryParams, 'idNumber', '')
      // 手动触发更新
      this.$forceUpdate()
      
    },

    onInputChange(e) {
      // 处理输入变化，确保清除按钮显示状态实时更新
      this.queryParams.idNumber = e.detail.value
    },

    calculateScrollHeight() {
      const query = uni.createSelectorQuery().in(this)
      query.select('.search-container').boundingClientRect(data => {
        if (data) {
          const searchHeight = data.height
          const windowHeight = uni.getSystemInfoSync().windowHeight
          this.scrollHeight = windowHeight - searchHeight - 100 // 100为其他元素的边距
        } else {
          // 如果 .search-container 不存在，设置一个默认高度
          const windowHeight = uni.getSystemInfoSync().windowHeight
          this.scrollHeight = windowHeight - 100 // 200为其他元素的边距
        }
      }).exec()
    },


    // 确定按钮点击事件
    handleIndex() {
      if (!this.queryParams.idNumber || this.queryParams.idNumber.trim() === '') {
        uni.showToast({
          title: '请输入身份证号',
          icon: 'none'
        });
        return;
      }
      
      // 简单验证身份证格式
      const idCardReg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
      if (!idCardReg.test(this.queryParams.idNumber)) {
        uni.showToast({
          title: '请输入正确的身份证号',
          icon: 'none'
        });
        return;
      }

      // 跳转到查询页面，传递身份证号码参数
      uni.navigateTo({
        url: `/pages/saishi/query?idNumber=${encodeURIComponent(this.queryParams.idNumber)}`
      });
    },

  },
  mounted() {
    this.calculateScrollHeight() // 计算滚动区域高度
  },
}
</script>

<style lang="scss">
@font-face {
  font-family: 'minikt';
  src: url('@/static/font/minikt.TTF') format('truetype');
}
.container {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.bg-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1; /* 让背景在底层 */
}



.text-container {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 40rpx;
  width: 100%; /* 确保文本容器占满父容器宽度 */
  z-index: 1;
}

.hint-text {
  color: #1051A8;
  font-size: 60rpx;
  font-weight: bold;
  font-family: 'minikt', sans-serif;
  text-align: center;
  line-height: 1.4;
  margin-bottom: 12rpx;
  width: 100%;
  display: block; /* 确保文本作为块级元素显示 */
  text-shadow: 0 3px 8px rgba(255, 255, 255, 1), 
               0 1px 2px rgba(255, 255, 255, 0.9),
               0 6px 12px rgba(255, 255, 255, 0.7); /* 更明显的多层白色阴影 */
}

.input-wrapper {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80%;
  height: 80rpx;
  background-color: #ffffff;
  border-radius: 30rpx;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 2rpx solid #8DA8CF;
  z-index: 10; /* 确保输入框在最上层 */
  display: flex;
  align-items: center;
  pointer-events: auto; /* 确保容器可以接收点击事件 */
  overflow: visible; /* 确保内容不被裁剪 */
}
.content-wrapper {
  position: absolute;
  top: 70%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80%;
  height: 80rpx;
  border-radius: 12rpx;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 10; /* 确保输入框在最上层 */
  display: flex;
  align-items: center;
}

.signup-text {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 3;
  color: #004AAC;
  font-size: 60rpx;
  font-weight: bold;
  font-family: 'minikt', sans-serif;
  text-align: center;
  display: block;
  border-radius: 10px;
}
.search-input {
  flex: 1;
  height: 100%;
  padding: 0 32rpx;
  padding-right: 80rpx; /* 为清除按钮留出空间 */
  font-size: 32rpx;
  color: #333333;
  background-color: transparent;
  border: none;
  outline: none;
  border-radius: 16rpx;
  box-sizing: border-box;
  line-height: 80rpx; /* 垂直居中对齐 */
  position: relative;
  z-index: 12; /* 确保输入框在最顶层 */
  pointer-events: auto; /* 确保可以接收点击事件 */
  touch-action: manipulation; /* 优化触摸响应 */
}

.clear-btn {
  position: absolute;
  right: 15rpx;
  top: 50%;
  transform: translateY(-50%);
  width: 50rpx;
  height: 50rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #cccccc;
  border-radius: 50%;
  cursor: pointer;
  z-index: 15; /* 提高z-index确保在最上层 */
  pointer-events: auto; /* 确保可以接收点击事件 */
  touch-action: manipulation; /* 优化触摸响应 */
  user-select: none; /* 防止选中 */
}

.clear-icon {
  color: #ffffff;
  font-size: 32rpx;
  font-weight: bold;
  line-height: 1;
}




</style>